package com.jinbo.footmessageworker.order;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jinbo.footmessageworker.BaseActivity;
import com.jinbo.footmessageworker.R;
import com.jinbo.footmessageworker.httpbean.OrderBean;
import com.jinbo.footmessageworker.httpbean.OrdersBean;
import com.jinbo.footmessageworker.httpretrofit.BusinessController;
import com.jinbo.footmessageworker.httpretrofit.HttpProtocolFactory;
import com.jinbo.footmessageworker.httpretrofit.Listener;
import com.jinbo.footmessageworker.utils.DialogUtils;
import com.jinbo.footmessageworker.utils.TimeUtils;
import com.jinbo.footmessageworker.view.CrossEditText;
import com.jinbo.footmessageworker.view.TitleBarView;

import de.hdodenhof.circleimageview.CircleImageView;


public class NotDealOrderDetailActivity extends BaseActivity implements View.OnClickListener {

    private static String ORDER_INTENT = "order_bean";

    //CircleImageView head;

    TextView mDateTV, mWeekTV, mTimeTv, mProjectsTv, mTotalPriceTv;
    TextView mContactNameTv, mContactPhoneTv, mContactAddressTv, mContactSexTv;
    Button left;
    Button right;
    public static void launch(Activity activity,OrderBean orderBean,int requestCode) {
        Intent intent = new Intent(activity, NotDealOrderDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ORDER_INTENT, orderBean);
        intent.putExtras(bundle);
        activity.startActivityForResult(intent, requestCode);
       // activity.startActivity(intent);
    }

    //int position;
    OrderBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  position = getIntent().getIntExtra("position", -1);
        setContentView(R.layout.activity_not_deal_order_detail);
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("订单详情");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);

       // head = (CircleImageView) findViewById(R.id.head);
        mDateTV = (TextView) findViewById(R.id.date);
        mWeekTV = (TextView) findViewById(R.id.week);
        mTimeTv = (TextView) findViewById(R.id.time);
        mProjectsTv = (TextView) findViewById(R.id.content);
        mTotalPriceTv = (TextView) findViewById(R.id.total_price);
      //  mNameTv = (TextView) findViewById(R.id.name);

        Intent intent = this.getIntent();
        bean = (OrderBean) intent.getSerializableExtra(ORDER_INTENT);
        mDateTV.setText(bean.date);
        mWeekTV.setText(bean.week);
        mTimeTv.setText(bean.begin_time+"-"+bean.end_time);
        mProjectsTv.setText(bean.content);//getProjectNames(bean.worker.projects));
        mTotalPriceTv.setText(bean.total_price+"元");//getPrice(bean.worker.projects) + "");
//        Glide.with(this)
//                .load(HttpProtocolFactory.HOST_URL_USER + bean.worker.head)
//                .into(head);
      //  mNameTv.setText(bean.worker.name);


        mContactNameTv = (TextView) findViewById(R.id.contact_name);
        mContactPhoneTv = (TextView) findViewById(R.id.contact_phone);
        mContactAddressTv = (TextView) findViewById(R.id.contact_address);
        mContactSexTv = (TextView) findViewById(R.id.contact_sex);


        mContactNameTv.setText(bean.contact_name);
        mContactPhoneTv.setText(bean.contact_phone);
        mContactAddressTv.setText(bean.contact_address);
        mContactSexTv.setText(bean.contact_sex);

        left=(Button)findViewById(R.id.left);
        right=(Button)findViewById(R.id.right);
        left.setOnClickListener(this);
        right.setOnClickListener(this);
    }

    Dialog dialog;
    DialogUtils dialogUtils;
    CrossEditText mInputEt;
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.left:
                BusinessController.getInstance().sureOrder(bean.id, TimeUtils.getNowTimeFormat(), new Listener<OrdersBean>() {
                    @Override
                    public void onStart(Object... params) {
                        showProgressDialog("请稍后...");
                        //mLoadView.setVisibility(View.VISIBLE);
                        //mPullRefreshLayout.setRefreshing(true);
                        // mLoadView.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onComplete(OrdersBean result, Object... params) {

                        dismissProgressDialog();

                        // mPullRefreshLayout.setRefreshing(false);
                        //  mLoadView.setVisibility(View.GONE);
                        //  mCallBacks1.change(result.orders);
                        // mCallBacks2.change(result.orders);
                        show("接收成功");
                        setResult(RESULT_OK);
                        finish();
                        //orders.remove(position);
                       // setorders(orders);

                    }

                    @Override
                    public void onFail(final String msg, Object... params) {
                        //.setVisibility(View.VISIBLE);
                        dismissProgressDialog();
                        //mLoadView.setVisibility(View.GONE);
                        // mPullRefreshLayout.setRefreshing(false);
                        if (TextUtils.isEmpty(msg)) {
                        } else {
                           show(msg);
                        }
                    }
                });
                break;
            case R.id.right:
                View parent= LayoutInflater.from(this).inflate(R.layout.dialog_content_input_text, null);
                mInputEt=(CrossEditText)parent.findViewById(R.id.input_text);
                dialogUtils= new DialogUtils(this,0,null, true);
                dialogUtils.setView(parent);
                dialog=dialogUtils.create();
                dialogUtils.setButtons(null, "确定",
                        new DialogUtils.OnDialogButtonClickListener() {
                            @Override
                            public void onDialogButtonClick(Context context, DialogUtils builder, Dialog dialog, int dialogId, int which) {
                                if (which == DialogUtils.OnDialogButtonClickListener.BUTTON_RIGHT) {


                                    BusinessController.getInstance().rejectOrder(bean.id, TimeUtils.getNowTimeFormat(), mInputEt.getText().toString(), new Listener<OrdersBean>() {
                                        @Override
                                        public void onStart(Object... params) {
                                           showProgressDialog("请稍后...");
                                            //mLoadView.setVisibility(View.VISIBLE);
                                            //mPullRefreshLayout.setRefreshing(true);
                                            // mLoadView.setVisibility(View.VISIBLE);
                                        }

                                        @Override
                                        public void onComplete(OrdersBean result, Object... params) {

                                            dismissProgressDialog();

                                            // mPullRefreshLayout.setRefreshing(false);
                                            //  mLoadView.setVisibility(View.GONE);
                                            //  mCallBacks1.change(result.orders);
                                            // mCallBacks2.change(result.orders);
                                            show("拒绝订单成功");
                                            setResult(RESULT_OK);
                                            finish();
                                            //orders.remove(position);
                                            //setorders(orders);

                                        }

                                        @Override
                                        public void onFail(final String msg, Object... params) {
                                            //.setVisibility(View.VISIBLE);
                                            dismissProgressDialog();
                                            //mLoadView.setVisibility(View.GONE);
                                            // mPullRefreshLayout.setRefreshing(false);
                                            if (TextUtils.isEmpty(msg)) {
                                            } else {
                                                show(msg);
                                            }
                                        }
                                    });

                                }
                            }
                        });
                dialog.show();
                break;
        }
    }
}

